﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Http;
using System.Web.Http;
using System.IO;

namespace ERPStore.Web.Exporter.Controllers
{
	public class AdminExporterApiController : ApiController
	{
		public AdminExporterApiController(ERPStore.Exporter.Services.IExporterService exporterService
			, ERPStore.Services.IRealtimeMessageService rtMessage
			, ERPStore.Logging.ILogger logger)
		{
			this.ExporterService = exporterService;
			this.RealtimeMessageService = rtMessage;
			this.Logger = logger;
		}

		protected ERPStore.Exporter.Services.IExporterService ExporterService { get; private set; }
		protected ERPStore.Services.IRealtimeMessageService RealtimeMessageService { get; private set; }
		protected ERPStore.Logging.ILogger Logger { get; private set; }

		[HttpGet]
		public DateTime Ping()
		{
			return DateTime.Now;
		}

		[ERPStore.Web.Controllers.ActionFilters.ApiAuthorizedOperation]
		[HttpGet]
		[HttpPost]
		public ERPStore.Exporter.Models.BanishedProductListResult GetBanishedProductList(ERPStore.Exporter.Models.BanishedProductListFilter filter)
		{
			var result = new ERPStore.Exporter.Models.BanishedProductListResult();
			int count = 0;
			var list = ExporterService.GetBanishedProductListByFilter(filter, out count);
			result.List = list;
			result.TotalCount = count;
			return result;
		}

		[ERPStore.Web.Controllers.ActionFilters.ApiAuthorizedOperation]
		[HttpPost]
		[HttpPut]
		public System.Net.Http.HttpResponseMessage EnqueueSaveBanishedProductList(ERPStore.Exporter.Models.SaveBanishedProductListOrder order)
		{
			var pr = CloudSoft.Workflows.ProgressReporterCollector.GetNew();

			StreamWriter streamWriter = null;
			var response = Request.CreateResponse();
			if (order.WithFeedback)
			{
				Request.Headers.AcceptEncoding.Clear();
				response.Content = new System.Net.Http.PushStreamContent((s, h, t) =>
					{
						streamWriter = new StreamWriter(s);
						streamWriter.Write("TaskId:" + pr.Id);
						streamWriter.Flush();
					}, "text/plain");
			}
			else
			{
				response.Content = new StringContent(pr.Id);
			}

			CloudSoft.Workflows.WorkflowQueueUserWorkItem.RunAsync(
				new ERPStore.Exporter.Workflows.Activities.SaveBanishedProductList()
				, new Dictionary<string, object>() { { "BanishedProductList", order.List } }
				, pr
				, (dic) =>
					{
						if (streamWriter != null)
						{
							streamWriter.Write("Success");
							streamWriter.Flush();
						}
					}
				, (ex) => 
					{
						if (streamWriter != null)
						{
							streamWriter.Write("Exception:" + ex.ToString());
							streamWriter.Flush();
						}
						Logger.Error(ex); 
					}
				, () =>
					{
						if (streamWriter != null)
						{
							streamWriter.Write("Finished");
							streamWriter.Flush();
							response.Dispose();
						}
					}
			);

			return response;
		}

		[ERPStore.Web.Controllers.ActionFilters.ApiAuthorizedOperation]
		[HttpPost]
		[HttpDelete]
		public bool DeleteBanishedProductList(List<ERPStore.Exporter.Models.BanishedProduct> banishedProductList)
		{
			ExporterService.DeleteBanishedProductList(banishedProductList);
			return true;
		}

		[ERPStore.Web.Controllers.ActionFilters.ApiAuthorizedOperation]
		[HttpPost]
		[HttpGet]
		public IDictionary<int, int> GetBanishmentCountByProductIdList(List<int> productIdList)
		{
			var result = ExporterService.GetBanishmentCountByProductIdList(productIdList);
			return result ?? new Dictionary<int, int>();
		}
	}
}
